package com.lipi.mall.coupon.service.impl;

import com.lipi.common.to.MemberPrice;
import com.lipi.common.to.SkuReductionTO;
import com.lipi.mall.coupon.entity.MemberPriceEntity;
import com.lipi.mall.coupon.entity.SkuLadderEntity;
import com.lipi.mall.coupon.service.MemberPriceService;
import com.lipi.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipi.common.utils.PageUtils;
import com.lipi.common.utils.Query;

import com.lipi.mall.coupon.dao.SkuFullReductionDao;
import com.lipi.mall.coupon.entity.SkuFullReductionEntity;
import com.lipi.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTO to) {
        //1、阶梯价格
        //sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();

        skuLadderEntity.setSkuId(to.getSkuId());
        skuLadderEntity.setFullCount(to.getFullCount());
        skuLadderEntity.setDiscount(to.getDiscount());
        skuLadderEntity.setAddOther(to.getCountStatus());
        if (to.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);

        }

        //2、满减
        //sms_sku_full_reduction

        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(to, skuFullReductionEntity);

        if (to.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            this.save(skuFullReductionEntity);
        }


        //3、会员价格
        // sms_member_price
        List<MemberPrice> memberPriceList = to.getMemberPrice();
        List<MemberPriceEntity> collect = memberPriceList.stream()
                .map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();

                    memberPriceEntity.setSkuId(to.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                }).filter(
                        item -> {
                            return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
                        }
                )
                .collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }
}